<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="login-left">
        <div class="login-banner">
          <h1>物业保修系统</h1>
          <p>专业的物业管理服务平台</p>
          <div class="features">
            <div class="feature-item">
              <el-icon><Tools /></el-icon>
              <span>快速保修</span>
            </div>
            <div class="feature-item">
              <el-icon><Clock /></el-icon>
              <span>实时跟踪</span>
            </div>
            <div class="feature-item">
              <el-icon><User /></el-icon>
              <span>专业服务</span>
            </div>
          </div>
        </div>
      </div>
      
      <div class="login-right">
        <div class="login-form-container">
          <div class="form-header">
            <h2>用户登录</h2>
            <p>欢迎回来，请登录您的账户</p>
          </div>
          
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            @submit.prevent
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                size="large"
                prefix-icon="User"
                clearable
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                size="large"
                prefix-icon="Lock"
                show-password
                clearable
                @keyup.enter.prevent="handleLogin"
              />
            </el-form-item>
            
            <el-form-item>
              <el-button
                type="primary"
                size="large"
                class="login-button"
                :loading="loading"
                @click.prevent="handleLogin"
                native-type="button"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
          
          <div class="login-footer">
            <p>
              还没有账号？
              <el-link type="primary" @click="goToRegister">立即注册</el-link>
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async (event) => {
  console.log('🚨 handleLogin函数开始执行')
  console.log('🚨 事件对象:', event)
  
  // 确保阻止默认行为
  if (event) {
    event.preventDefault()
    event.stopPropagation()
  }
  
  if (!loginFormRef.value) {
    console.log('🚨 loginFormRef不存在，函数退出')
    return
  }
  
  console.log('🚨 开始表单验证')
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        console.log('🎯 =================================')
        console.log('🎯 登录按钮点击')
        console.log('🎯 登录表单数据:', loginForm)
        
        await userStore.login(loginForm)
        
        console.log('🎯 userStore.login完成')
        console.log('🎯 检查token - localStorage:', localStorage.getItem('token'))
        console.log('🎯 检查token - store:', userStore.token)
        console.log('🎯 检查isLoggedIn - store:', userStore.isLoggedIn)
        
        ElMessage.success('登录成功')
        
        console.log('🎯 准备跳转到dashboard')
        console.log('🎯 当前路由:', router.currentRoute.value.path)
        
        // 确保状态已更新，然后跳转到首页
        setTimeout(() => {
          console.log('🎯 开始跳转...')
          console.log('🎯 跳转前最后检查token:', localStorage.getItem('token'))
          router.replace('/dashboard')
          console.log('🎯 router.replace调用完成')
        }, 100)
        
        console.log('🎯 =================================')
      } catch (error) {
        ElMessage.error(error.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  overflow: hidden;
  margin: 0;
  padding: 0;
  z-index: 1;
}

.login-wrapper {
  display: flex;
  width: 100%;
  height: 100%;
}

.login-left {
  flex: 0 0 60%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px 40px;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 1px, transparent 1px);
  background-size: 50px 50px;
  animation: float 20s infinite linear;
}

@keyframes float {
  0% { transform: translate(0, 0) rotate(0deg); }
  100% { transform: translate(-50px, -50px) rotate(360deg); }
}

.login-banner {
  color: white;
  text-align: center;
  position: relative;
  z-index: 1;
  max-width: 600px;
}

.login-banner h1 {
  font-size: 48px;
  font-weight: 800;
  margin-bottom: 20px;
  text-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  line-height: 1.1;
  letter-spacing: -1px;
}

.login-banner p {
  font-size: 18px;
  margin-bottom: 40px;
  opacity: 0.9;
  line-height: 1.4;
  font-weight: 300;
}

.features {
  display: flex;
  justify-content: space-between;
  margin-top: 50px;
  gap: 30px;
}

.feature-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  flex: 1;
}

.feature-item .el-icon {
  font-size: 36px;
  color: rgba(255, 255, 255, 0.9);
  padding: 16px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  backdrop-filter: blur(10px);
}

.feature-item span {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.9);
  font-weight: 500;
  text-align: center;
}

.login-right {
  flex: 0 0 40%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 30px;
  background: white;
  position: relative;
}

.login-right::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 100%;
  background: linear-gradient(to bottom, transparent, rgba(0,0,0,0.1), transparent);
}

.login-form-container {
  width: 100%;
  max-width: 350px;
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.form-header h2 {
  font-size: 28px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 8px;
  letter-spacing: -0.5px;
}

.form-header p {
  font-size: 16px;
  color: #909399;
  line-height: 1.5;
  font-weight: 400;
}

.login-form {
  margin-bottom: 30px;
}

.login-form .el-form-item {
  margin-bottom: 24px;
}

.login-form .el-input {
  height: 56px;
}

.login-form .el-input__wrapper {
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  border: 2px solid #f0f2f5;
  transition: all 0.3s ease;
  background: #fafbfc;
}

.login-form .el-input__wrapper:hover {
  border-color: #409eff;
  box-shadow: 0 4px 16px rgba(64, 158, 255, 0.15);
  background: white;
}

.login-form .el-input__wrapper.is-focus {
  border-color: #409eff;
  box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1);
  background: white;
}

.login-button {
  width: 100%;
  height: 56px;
  font-size: 18px;
  font-weight: 600;
  border-radius: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  letter-spacing: 0.5px;
}

.login-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 24px rgba(102, 126, 234, 0.4);
}

.login-button:active {
  transform: translateY(-1px);
}

.login-footer {
  text-align: center;
  margin-top: 30px;
}

.login-footer p {
  color: #909399;
  font-size: 15px;
  font-weight: 400;
}

.login-footer .el-link {
  font-weight: 600;
  font-size: 15px;
}

/* 大屏幕优化 */
@media (min-width: 1400px) {
  .login-banner h1 {
    font-size: 72px;
  }
  
  .login-banner p {
    font-size: 28px;
  }
  
  .feature-item .el-icon {
    font-size: 56px;
  }
  
  .feature-item span {
    font-size: 20px;
  }
  
  .form-header h2 {
    font-size: 40px;
  }
  
  .form-header p {
    font-size: 18px;
  }
}

/* 中等屏幕适配 */
@media (max-width: 1200px) {
  .login-left {
    flex: 0 0 55%;
    padding: 60px;
  }
  
  .login-right {
    flex: 0 0 45%;
    padding: 60px 40px;
  }
  
  .login-banner h1 {
    font-size: 48px;
  }
  
  .login-banner p {
    font-size: 20px;
  }
  
  .features {
    margin-top: 60px;
    gap: 30px;
  }
  
  .feature-item .el-icon {
    font-size: 40px;
  }
  
  .feature-item span {
    font-size: 16px;
  }
}
</style>